package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.FandV;
import cn.itcast.travel.service.UserService;
import cn.itcast.travel.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Map;

@WebServlet("/fmohuchaxunServlet")
public class FmohuchaxunServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = new String(request.getParameter("name").getBytes("iso-8859-1"), "utf-8");
        /*String name = request.getParameter("name");*/
        /*Map<String, String[]> map = request.getParameterMap();*/
        /*System.out.println("map:");
        System.out.println(map.toString());
        System.out.println("-----------------------");

        System.out.println(name);*/

        /*String[] sm = new String[] {name};

        System.out.println("sm："+sm[0]);
*/

        FandV fandV = new FandV();
        fandV.setName(name);
       /* try {
            BeanUtils.populate(fandV,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }*/

        /*System.out.println("servlet1");
        System.out.println(fandV);*/

        UserService service = new UserServiceImpl();
        /*FandV fandV1 = */service.Fmohuchaxun(fandV);

       /* System.out.println("servlet2");
        System.out.println(fandV);

        System.out.println(map);*/
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        mapper.writeValue(response.getOutputStream(),service.Fmohuchaxun(fandV)/*fandV*/);
        /*System.out.println("输出");
        System.out.println("输出结果："+service.Fmohuchaxun(fandV));
        System.out.println("结果");
*/
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
